package com.yskj.mwk.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yskj.mwk.bean.JtTrajectory;
import com.yskj.mwk.service.ManagerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("wuliu")
public class WuliuController {

    private static final Logger logger = LoggerFactory.getLogger(WuliuController.class);

    @Autowired
    private ManagerService managerService;

    @PostMapping("/jtOrderCallback")
    public Map jtOrderCallback(@RequestParam Map<String, String> params) {
        logger.info("【物流订单回调】{}", params);
        Map<String, Object> result = new HashMap();
        try {
            System.out.println(params);
            result.put("code", "1");
            result.put("msg", "success");
            result.put("data", "SUCCESS");
        } catch (Exception e) {
            result.put("code", "0");
            result.put("msg", "系统错误");
            result.put("data", "系统错误");
        }
        return result;
    }

    @PostMapping("/jtTrajectoryCallback")
    public Map jtTrajectoryCallback(@RequestParam Map<String, String> params) {
        logger.info("【物流轨迹回调】{}", params);
        Map<String, Object> result = new HashMap();

        try {
            System.out.println(params);
            // 解析逻辑
            if (params.containsKey("bizContent")) {
                String bizContentObj = params.get("bizContent")+"";
                System.out.println(bizContentObj);
                // 1. 创建 ObjectMapper
                ObjectMapper mapper = new ObjectMapper();

                // 2. 解析 JSON
                JtTrajectory jtTrajectory = mapper.readValue(bizContentObj, JtTrajectory.class);

                // 3. 遍历 details
                if (jtTrajectory.getDetails() != null) {
                    for (JtTrajectory.Detail detail : jtTrajectory.getDetails()) {
                        System.out.println("Desc: " + detail.getDesc());
                        System.out.println("ScanType: " + detail.getScanType());
                        System.out.println("ScanTime: " + detail.getScanTime());
                        System.out.println("BillCode: " + detail.getBillCode());
                        managerService.addWuliuGuiji(detail.getBillCode(),detail.getScanTime(),detail.getScanType(),detail.getDesc());
                        System.out.println("-------------------");
                    }
                }
            } else {
                System.err.println("缺少 bizContent 字段");
            }
            result.put("code", "1");
            result.put("msg", "success");
            result.put("data", "SUCCESS");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", "0");
            result.put("msg", "系统错误");
            result.put("data", "系统错误");
        }
        return result;
    }
}
